﻿/*
Copyright (c) 2013, Marc Tanti
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of the www.marctanti.com nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using MARIA.level.direction;
using MARIA.level.square;
using MARIA.level.situation;

namespace MARIA.level.robot
{
    public class Robot
    {

        /// <summary>
        /// The image which displays the robot.
        /// </summary>
        public Image RobotImage
        {
            get
            {
                return robotImage;
            }
        }
        private Image robotImage;

        /// <summary>
        /// The current location of the robot.
        /// </summary>
        public Point CurrentLocation
        {
            get
            {
                return currentLocation;
            }
        }
        private Point currentLocation;

        /// <summary>
        /// The initial location the robot was in.
        /// </summary>
        private Point initialLocation;

        /// <summary>
        /// The situation this robot resides in.
        /// </summary>
        private Situation ownerSituation;

        /// <summary>
        /// Observer for when the robot moves.
        /// </summary>
        /// <param name="sender">The robot object which fires the event.</param>
        /// <param name="args">The event arguments.</param>
        public delegate void RobotMovedHandler(object sender, RobotMovedEventArgs args);
        
        /// <summary>
        /// Event which is fired when the robot moves.
        /// </summary>
        public event RobotMovedHandler OnRobotMoved;

        /// <summary>
        /// Initialize a new instance of Robot using a situation the robot resides in and an initial location.
        /// </summary>
        /// <param name="ownerSituation">The owning situation.</param>
        /// <param name="initialLocation">The initial location.</param>
        public Robot(Situation ownerSituation, Point initialLocation)
        {
            this.ownerSituation = ownerSituation;
            this.initialLocation = initialLocation;
            this.currentLocation = initialLocation;
            this.robotImage = Properties.Resources.robot;
        }

        /// <summary>
        /// Reinitialize the robot's state and location.
        /// </summary>
        public void Reinitialize()
        {
            currentLocation = initialLocation;
            robotImage = Properties.Resources.robot;
        }

        /// <summary>
        /// Move in a particular direction.
        /// </summary>
        /// <param name="direction">The direction to move in.</param>
        public void Move(Direction direction)
        {
            Point oldLocation = currentLocation;
            currentLocation = direction.NewLocationInThisDirection(currentLocation);
            OnRobotMoved(this, new RobotMovedEventArgs(oldLocation, currentLocation, direction));
        }

        /// <summary>
        /// See what is the square in a particular direction is.
        /// </summary>
        /// <param name="direction">The direction to see in.</param>
        /// <returns>The square.</returns>
        public Square See(Direction direction)
        {
            Point locationToSee = direction.NewLocationInThisDirection(currentLocation);
            return ownerSituation.GetSquare(locationToSee);
        }

    }
}
